package com.lanmao.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lanmao.blog.entity.User;
import com.lanmao.blog.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class CustomUserServiceImpl implements UserDetailsService {
    @Autowired
    UserMapper userMapper;
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //User user = userMapper.selectOne(new QueryWrapper<User>().eq("username",s).or().eq("phone",s));
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("phone",s));
        if(user==null){
            user = userMapper.selectOne(new QueryWrapper<User>().eq("username",s));
            if (user == null) {  //如果用户名不存在，就抛出下面的信息
                throw new UsernameNotFoundException("用户名不存在！");
            }
        }

        //用户存在的话就返回hr对象
        return user;
    }
}
